<?php if ( ! defined('BASEPATH')) exit('This file does not run directly. Sorry.'); ?>
<?php
/**
 * Kalaisoo.
 *
 * A lightweight no-framework framework written in PHP.
 *
 * @package Kalaisoo
 * @author $Author: info@sah-company.com $
 */
?>
<?php
/**
 * scaffold_cells.
 *
 * @param array The gestalt array of the model
 * @param Model The model or a record of a certain model
 * @param string The html token to wrap around
 * @param bool A flag to decide if value is taken from the record or made as a label
 * @param array A key/value array with pagination information
 * @param mixed The line number, starting with 0
 */
function scaffold_cells(array $gestalt, $record = null, $url = null, $token = 'td', $from_record = true, array $pagination = array(), $line = null)
{
	$number_of_cells = 0;
	$orderDirections = array(
		0 => 'asc',
		1 => 'desc'
	);
	$urlSortableSchema = $url.'/%d/%d/%d/%d'; // this will be our uri later on
	
	// make a first cell
	if ($from_record) {
		// make the link
		$link = '<a href="'.ahref(sprintf($url, $record->getId())).'" title="'.sprintf(__('scaffold_open_link'), urlencode($record->getId())).'" class="pencil"><span>'.__('scaffold_open').'</span></a>';
		//$link = '<a href="'.ahref(sprintf($urlSortableSchema, $record->getId(), $line, 1, $pagination['orderField'], $pagination['orderDirection'])).'" title="'.sprintf(__('scaffold_open_link'), $record->getId()).'"><span>'.__('open').'</span></a>';
		echo "<{$token} class=\"handle\">{$link}</{$token}>\n";		
	}
	else {
		echo "<{$token} class=\"handle\">&nbsp;</{$token}>\n";
	}
	// for all fields in the record we make cells
	foreach ($gestalt as $n=>$field_def) {
		if (isset($field_def['visibility']['table']) && $field_def['visibility']['table'] == false) {
			continue; // skip this in table
		}
		$number_of_cells++;
		$style = null;
		$link = null;
		
		// glue th/td array of fielddefintion
		$stack = null;
		if (isset($field_def[$token])) {
			$stack = glue($field_def[$token]);
		}
		
		if ($from_record) {
			/**
			 * Display a records cell data in tbody cell
			 */
			$rawValue = htmlspecialchars($record->$field_def['name']);
			$value = '<span title="'.$rawValue.'">'.$rawValue.'</span>';
			if (isset($field_def['selectable']) && $field_def['selectable']) {
				// and the checkbox
				$cb_name = 'selection['.$record->getMeta('type').']['.$record->getId().']';
				$cb_id = 'selection_'.$record->getMeta('type').'_'.$record->getId();
				$value = '<input type="checkbox" id="'.$cb_id.'" name="'.$cb_name.'" value="'.$record->getId().'" class="selection" /> <label for="'.$cb_id.'" class="selection">'.$link.$value.'</label>';
			}
		}
		else {
			/**
			 * Display a cell in thead, with sort link and css classes
			 */
			$rawValue = __($record->getMeta('type').'_'.$field_def['name'].'_label');
			$value = '<span title="'.$rawValue.'">'.$rawValue.'</span>';
			// is that column sortable?
			if (isset($field_def['sortable']) && $field_def['sortable']) {
				$classes = 'sortable';
				if ($pagination['orderField'] == $n) {
					$classes .= ' active';
					$newOrderDirection = $pagination['orderDirection'] ^ 1;
					$classes .= ' '.$orderDirections[$pagination['orderDirection']];
					$urlSortable = sprintf($urlSortableSchema, urlencode($record->getId()), $pagination['currentPage'], $pagination['recordsPerPage'], $n, $newOrderDirection);
				}
				else {
					$urlSortable = sprintf($urlSortableSchema, urlencode($record->getId()), $pagination['currentPage'], $pagination['recordsPerPage'], $n, $pagination['orderDirection']);
				}
				$value = '<a href="'.ahref($urlSortable).'" class="'.$classes.'">'.$value.'</a>';
			}
		}
		echo "<{$token}{$stack}>{$value}</{$token}>\n";
	}
	return $number_of_cells;
}
?>
<section class="list">
<h1><?php echo __($scaffold['model']->getMeta('type').'_name'); ?></h1>
<form id="<?php echo $action; ?>" method="post" action="<?php echo ahref($url); ?>" accept-charset="utf-8">
	<div>
	<table>
		<thead>
			<tr>
<?php
$number_of_cells = scaffold_cells($scaffold['model']->gestalt('table'), $scaffold['model'], $urlIndex, 'th', false, $pagination);
?>
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td class="handle"></td>
				<td colspan="<?php echo $number_of_cells; ?>">
					<input type="checkbox" id="select-all" name="select_all" value="1" title="<?php echo __('scaffold_select_all_title'); ?>" onclick="toggleCheckboxes(this.checked == true);" />
<?php
if (isset($menu)):
	$selMenu = new FeatureSelect(null, 'action', null, array('id' => 'select-action', 'options' => $menu));
	$gestalt->add($selMenu);
	//echo select('action', null, array('id' => 'select-action', 'options' => $menu));
	echo $selMenu->draw();
endif;
?>
				<input type="submit" name="submit" value="<?php echo $scaffold['submit_button']; ?>" />
				</td>
			</tr>
		</tfoot>
		<tbody>
<?php
/**
 * For every record found a table row is generated. The linenumbers are counted starting
 * from zero. The linenumber together with the page number is used to calculate the index.
 */
$n = 0;
$offset = ($pagination['currentPage'] - 1) * $pagination['recordsPerPage'] + 1;
foreach ($scaffold['records'] as $id=>$record):
	$line = $offset + $n++;
?>
<tr class="record">
<?php
scaffold_cells($record->gestalt('table'), $record, $urlView, 'td', true, $pagination, $line);
?>
</tr>
<?php
endforeach;
?>
		</tbody>
	</table>
	</div>
</form>
<?php
echo $pagination['object']->output();
?>
</section>